//
//  XHPaggingNavbar.m
//  XHTwitterPagging
//
//  Created by 曾 宪华 on 14-6-20.
//  Copyright (c) 2014年
//

#import "XHPaggingNavbar.h"

#define kXHLabelBaseTag 1000

#define kXHRadie 3.2

#define kXHwidth 60

@interface XHPaggingNavbar () {
    CGFloat _mainScreenWidth;
    CGFloat lin_width;
}

/**
 *  title label 集合
 */
@property (nonatomic, strong) NSMutableArray *Buttons;

@property (nonatomic, strong) UIView         *Line_bottom;

@end

@implementation XHPaggingNavbar

#pragma mark - DataSource

- (void)reloadData {
    if (!self.titles.count) {
        return;
    }
    
    [self.Buttons enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
        label.hidden = YES;
    }];

    CGFloat normalWidth = (RECRWITCH-110)/2;
    
    [self.titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
        CGRect titleLabelFrame = CGRectMake(normalWidth - self.titles.count*kXHwidth/2+(idx * kXHwidth), 8, kXHwidth, 25);
        NSInteger tag = kXHLabelBaseTag + idx;
        UIButton *button = (UIButton *)[self viewWithTag: tag];
        if (!button) {
            button = [[UIButton alloc] init];
            button.tag = tag;
            [self addSubview:button];
            [self.Buttons addObject:button];
        }
        button.hidden = NO;
        button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UINavigationBar appearance].tintColor forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        button.frame = titleLabelFrame;
        [button addTarget:self action:@selector(choosetype:) forControlEvents:UIControlEventTouchUpInside];
//        titleLabel.font = [[UINavigationBar appearance].titleTextAttributes objectForKey:NSFontAttributeName];
        if (self.currentPage == idx) {
            button.titleLabel.font=[UIFont boldSystemFontOfSize:17];
        } else {
            button.titleLabel.font=[UIFont systemFontOfSize:16];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }];
    
    _Line_bottom = [UIView new];
    _Line_bottom.frame=CGRectMake(self.bounds.size.width/2 - self.titles.count*kXHwidth/2 +15, 38, 30, 2);
    _Line_bottom.layer.cornerRadius=1.0;
    _Line_bottom.backgroundColor=NavItemTitleColor;
    [self addSubview:_Line_bottom];
}

- (void)choosetype:(UIButton *)sender{
    [self setCurrentPage:sender.tag-kXHLabelBaseTag];
    
    _didChangedIndex (sender.tag-kXHLabelBaseTag);
}

#pragma mark - Propertys

- (void)setCurrentPage:(NSInteger)currentPage {
    _currentPage = currentPage;
    
    [self.Buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) {
        if ([button isKindOfClass:[UIButton class]]) {
            
            if (self->_currentPage == idx) {
                button.titleLabel.font=[UIFont boldSystemFontOfSize:17];
                [button setTitleColor:[UINavigationBar appearance].tintColor forState:UIControlStateNormal];
            } else {
                button.titleLabel.font=[UIFont systemFontOfSize:16];
                [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }
        }
    }];

}

- (void)setContentOffset:(CGPoint)contentOffset {
    _contentOffset = contentOffset;
    
    CGFloat xOffset = contentOffset.x;
    
    CGFloat x_lin = self.bounds.size.width/2 - self.titles.count*kXHwidth/2+((xOffset/RECRWITCH) * kXHwidth + 15);
    _Line_bottom.frame=CGRectMake(x_lin, 38, 30, 2);
}

- (NSMutableArray *)Buttons {
    if (!_Buttons) {
        _Buttons = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _Buttons;
}

#pragma mark - Life Cycle

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _mainScreenWidth = [[UIScreen mainScreen] bounds].size.width;
    }
    return self;
}

- (void)dealloc {
    self.Buttons = nil;
}

@end
